<?php
/**
 * RmaRepo.php
 *
 * @copyright  2023 beikeshop.com - All Rights Reserved
 * @link       https://beikeshop.com
 * @author     TL <mengwb@guangda.work>
 * @created    2023-05-31 20:42:05
 * @modified   2023-05-31 20:42:05
 */

namespace Beike\Seller\Repositories;

use Beike\Models\Rma;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

class RmaRepo extends \Beike\Repositories\RmaRepo
{
    /**
     * @param $id
     * @param int $sellerId
     * @return Model|Collection|Builder|array|null
     */
    public static function find($id, int $sellerId = 0): Model|Collection|Builder|array|null
    {
        if (! $sellerId) {
            $sellerId = current_seller_user()->seller->id ?? 0;
        }

        return Rma::query()->whereHas('order', function ($query) use ($sellerId) {
            $query->where('seller_id', $sellerId);
        })->find($id);
    }

    /**
     * @param $data
     * @param int $sellerId
     * @return LengthAwarePaginator
     */
    public static function list($data, int $sellerId = 0): LengthAwarePaginator
    {
        if (! $sellerId) {
            $sellerId = current_seller_user()->seller->id ?? 0;
        }

        return self::getBuilder($data)->whereHas('order', function ($query) use ($sellerId) {
            $query->where('seller_id', $sellerId);
        })->paginate(perPage())->withQueryString();
    }
}
